import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import VueClipBoard from 'vue-clipboard2'
import Vant from 'vant';
import {
  Toast,
  Dialog
} from 'vant';
import 'vant/lib/index.css';
import 'vant/lib/index.less';
import 'animate.css'
import './assets/css/reset.css'

Vue.use(Vant);
Vue.use(VueClipBoard)

Vue.config.productionTip = false
Vue.prototype.$toast = Toast
Vue.prototype.$dialog = Dialog

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires=" + d.toGMTString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i].trim();
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function checkCookie() {
  var user = getCookie("username");
  if (user != "") {
    alert("欢迎 " + user + " 再次访问");
  } else {
    user = prompt("请输入你的名字:", "");
    if (user != "" && user != null) {
      setCookie("username", user, 30);
    }
  }
}

Vue.prototype.$setCookie = setCookie
Vue.prototype.$getCookie = getCookie
Vue.prototype.$checkCookie = checkCookie

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const hasUserInfo = localStorage.getItem('token') ? localStorage.getItem('token') : false
    console.log(localStorage.getItem('userId'))
    console.log(store.state.userId)
    // if (hasUserInfo == store.state.token && localStorage.getItem('userId') == store.state.userId) {
    if (hasUserInfo) {
      next()
    } else {
      if(to.path == '/agreement') return next()
      // console.log('------------', to.path)
      Toast.fail('请登录');
      next({
        name: 'login'
      })
      // router.push('/login')
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')